﻿/*
 * libusb example program to list devices on the bus
 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include "..\libusb.h" 
#define MY_VID 0x0483
#define	MY_PID 0x1894

#define	EP_OUT	0x01
#define	EP_IN	0x81
#define	BUFSIZE	128
unsigned char TX_BUF[BUFSIZE] = "hangzhoulangjie";
unsigned char RX_BUF[BUFSIZE];
int main(void)
{
	libusb_device** devs;
	libusb_device* dev;
	libusb_device_handle* handle = NULL;
	libusb_context* ctx = NULL;
	int device_cnt = 0;
	int r = 0;
	int i = 0;
	ssize_t cnt;
	int act_length = 0;
	for (i = 0; i < BUFSIZE; i++)
		TX_BUF[i] = rand() % 1000;

	r = libusb_init(&ctx);
	if (r < 0) {
		printf("fail to init\n");
		return 1;
	}
	libusb_set_debug(ctx, 3);

	cnt = libusb_get_device_list(ctx, &devs);
	if (cnt < 0) {
		libusb_exit(NULL);
		return (int)cnt;
	}

	//while ((dev = devs[i++]) != NULL) {
	//	struct libusb_device_descriptor desc;
	//	int r = libusb_get_device_descriptor(dev, &desc);
	//	if (r < 0) {
	//		fprintf(stderr, "failed to get device descriptor");
	//		return r;
	//	}

	//	printf("%04x:%04x (bus %d, device %d)",
	//		desc.idVendor, desc.idProduct,
	//		libusb_get_bus_number(dev), libusb_get_device_address(dev));

	//	if (desc.idProduct == MY_PID && desc.idVendor == MY_VID) {
	// 		device_cnt++;
	//		r = libusb_open(dev, &handle);
	//		if (r < 0) 
	//			printf("fail to open device\n");
	//		else
	//			printf("success to open device\n");
	//		break;
	//	}
	//	printf("\n");
	//}

	handle = libusb_open_device_with_vid_pid(ctx, MY_VID, MY_PID);
	if (handle == NULL)
		printf("cannot open device\n");
	else
		printf("open device\n");

	libusb_free_device_list(devs, 1);
	
	r = libusb_kernel_driver_active(handle, 1);
	if (r == 1) {
		printf("device is actived\n");
		libusb_detach_kernel_driver(handle, 1);
		if (r == 0)
			printf("device is detached\n");
	}

	r = libusb_claim_interface(handle, 1);
	if (r < 0) {
		printf("fail to claim interface\n");
		return 1;
	}
	int opt = -1;

	printf("success to claim interface\n");
	while (opt != 0)
	{
		printf("------------------------------------\n");
		printf("            0:退出程序\n");
		printf("            1:写数据\n");
		printf("            2:读数据\n");
		printf("            3:写读并校验数据\n");
		printf("请选择： \n");
		printf("------------------------------------\n");
		scanf_s("%d", &opt);
		
		switch (opt)
		{
		case 0:
			break;
		case 1:		
			r = libusb_bulk_transfer(handle, EP_OUT, TX_BUF, BUFSIZE, &act_length, 0);
			printf("success to write,write %d bytes to device\n", act_length);
			break;
		case 2:
			r = libusb_bulk_transfer(handle, EP_IN, RX_BUF, BUFSIZE, &act_length, 0);
			printf("success to read,read %d bytes from device\n", act_length);
			break;
		case 3:
			for (i = 0; i < BUFSIZE; i++)
				TX_BUF[i] = rand() % 32767;
			r = libusb_bulk_transfer(handle, EP_OUT, TX_BUF, BUFSIZE, &act_length, 0);
			r = libusb_bulk_transfer(handle, EP_IN, RX_BUF, BUFSIZE, &act_length, 0);
			for (i = 10; i > 0; i--) {
				if (TX_BUF[BUFSIZE - i] == RX_BUF[BUFSIZE - i])
					printf("num %d data is %d\n", BUFSIZE - i + 1, TX_BUF[BUFSIZE - i]);
				else {
					printf("check error\n");
					break;
				}
			}
		default:
			printf("请重新输入");
			break;
		}

	}
	libusb_release_interface(handle, 0);
	libusb_close(handle);
	libusb_exit(ctx);
	return 0;
}
